﻿
CREATE PROCEDURE sp_InsertFilteredEvent
	-- Add the parameters for the stored procedure here
	@MeterNumber nvarchar(50),
	@MeterVendorID nvarchar(50),
	@MeterHeadEndID nvarchar(50),
	@TimeStamp datetime,
	@EventType nvarchar(50),
	@Reading float,
	@Temp float,
	@Code nvarchar(50),
	@Status nvarchar(50)
AS
BEGIN
DECLARE @Count int

	SELECT
		--[MeterNumber]
	 --  ,[MeterVendorID]
	 --  ,[MeterHeadEndID]
	 --  ,[TimeStamp]
	 --  ,[EventType]
	 --  ,[Reading]
	 --  ,[Temp]
	 --  ,[Code]
	 --  ,[Status]
	 @Count = COUNT(*)
	FROM	
		[AMI].[dbo].[FilteredEvents] WITH(NOLOCK)
	WHERE
		[MeterNumber]=@MeterNumber and
		[MeterVendorID] = @MeterVendorID and
		[MeterHeadEndID] = @MeterHeadEndID and
		[EventType] =@EventType and
		[Status] = @Status
		
	If @Count=0
		BEGIN	
			INSERT INTO [AMI].[dbo].[FilteredEvents] WITH(ROWLOCK)
				   ([MeterNumber]
				   ,[MeterVendorID]
				   ,[MeterHeadEndID]
				   ,[TimeStamp]
				   ,[EventType]
				   ,[Reading]
				   ,[Temp]
				   ,[Code]
				   ,[Status])
			 VALUES
				   (
				   @MeterNumber,
				   @MeterVendorID,
				   @MeterHeadEndID,
				   @TimeStamp,
				   @EventType,
				   @Reading,
				   @Temp,
				   @Code,
				   @Status
				   )
				   

		END
	Else
		BEGIN
			SELECT
				 @MeterNumber='x'
				,@MeterVendorID='x'
				,@MeterHeadEndID='x'
				,@EventType='x'
				,@Reading='0'
				,@Temp='0'
				,@Code='x'
				,@Status='x'
		END
		
	SELECT
		 [MeterNumber]=@MeterNumber
		,[MeterVendorID]=@MeterVendorID
		,[MeterHeadEndID]=@MeterHeadEndID
		,[TimeStamp]=@TimeStamp
		,[EventType]=@EventType
		,[Reading]=@Reading
		,[Temp]=@Temp
		,[Code]=@Code
		,[Status]=@Status

END
